'use strict'

angular.module('questionList', ['ui.bootstrap'])
    .component('questionList', {
        templateUrl: 'components/questionList/questionList.template.html',
        controller: function ($scope, $http, $state, $timeout, $modal, ErrorService) {
            $scope.url = '/exam/v1/questions/';

            $scope.maxSize = 5;
            $scope.totalNum = 0;
            $scope.itemsPerPage = 10;
            $scope.currentPage = 1;
            /*加载标志*/
            $scope.isLoading = true;

            /*搜索标志*/
            $scope.isSearch = false;

            /*试卷列表*/
            $scope.questions = null;

            /*保存搜索框内容*/
            $scope.questionName = "";

            //查看题目信息
            var infoModal = $modal({
                scope: $scope,
                templateUrl: 'components/questionList/questionInfo.template.html',
                show: false
            });

            //编辑题目信息
            var editModal = $modal({
                scope: $scope,
                templateUrl: 'components/questionList/editQuestion.template.html',
                show: false
            });

            /*===============================================*/

            /**
             * 刷新页面
             */
            var reloadState = function() {
                $state.reload();
            };

            $scope.showInfoModal = function(id) {
                //根据id查询题目信息
                if (id != null) {
                    $http.get("/questions/info?id=" + id).then(function(response) {
                        //赋值
                        $scope.question = response.data;
                        //显示查询信息
                        infoModal.$promise.then(infoModal.show);
                    }, function(error) {
                        ErrorService.error(error);
                    });
                }
            };


            $scope.showEditModal = function(id) {
                if (id != null) {
                    $http.get("/questions/info?id=" + id).then(function(response) {
                        //赋值
                        $scope.question = response.data;
                        //显示查询信息
                        editModal.$promise.then(editModal.show);
                    }, function (error) {
                        ErrorService.error(error);
                    });
                }
            };

            /*更新题目*/
            $scope.updateQuestion = function () {
                $http.put('/questions/update', $scope.question).then(function (response) {
                    //保存成功
                    $(".save-success").removeClass("in").show();
                    $(".save-success").delay(200).addClass("in").fadeOut(2000);
                    //隐藏弹窗
                    $timeout(function () {
                        editModal.$promise.then(editModal.hide);
                        reloadState();
                    }, 2000);

                }, function (error) {
                    //保存失败
                    $(".save-fail").removeClass("in").show();
                    $(".save-fail").delay(200).addClass("in").fadeOut(2000);
                    //隐藏弹窗
                    $timeout(function () {
                        editModal.$promise.then(editModal.hide);
                        reloadState();
                    }, 2000);
                });
            };

            $scope.deleteQuestion = function(id) {

                /*测试*/
                if (confirm('是否删除?')) {
                    /*http delete 后台*/
                    $http.post("/questions/delete", {id: id}).then(function (response) {
                        console.log(response);
                    }, function (error) {
                        ErrorService.error(error);
                    });
                    reloadState();
                }
            };

            /* 以分页方式获取试卷列表 */
            $scope.searchQuestions = function () {
                $scope.isLoading = true;

                $http.get('/questions/list?pageIndex=' + $scope.currentPage + '&pageSize=' + $scope.itemsPerPage + '&title=' + $scope.questionName).then(function (result) {
                    if (result.data.status === 1) {
                        $scope.questions = result.data.data.list;
                        $scope.totalNum = result.data.data.total;
                    }
                    $scope.isLoading = false;
                });
            };

            $scope.addQuestion = function() {
                $state.go('add-question');
            };

            $scope.pageChanged = function() {
                $scope.searchQuestions();
            };
            $scope.searchQuestions();
        }
    });